var objOkTab = null;
layui.use(["element", "form", "layer", "okUtils", "okTab", "okLayer", "okContextMenu", "okHoliday", "laydate"], function () {
	let okUtils = layui.okUtils;
	let $ = layui.jquery;
	let form = layui.form;
	let layer = layui.layer;
	let okLayer = layui.okLayer;
	let okHoliday = layui.okHoliday;
	let config = okUtils.local("okConfig") || okConfig || {};
	okLoading && okLoading.close();
	/**
	 * 关闭加载动画
	 */
	$(".layui-layout-admin").removeClass("orange_theme blue_theme");
	$(".layui-layout-admin").addClass(config.theme);
	if (config.menuArrow) {
		$("#navBar").addClass(config.menuArrow);
	}
	/**
	 * 左侧导航栏
	 */
	let okTab = null;
	$("body").on("click", ".sysModelLi", function () {
		let mid = $(this).attr("mid");
		if (objOkTab != null && objOkTab.tabConfig.modelId === mid) {
			return;
		}
		$(".sysModelLi button").removeClass("layui-btn-normal").addClass("layui-btn-warm");
		$(this).find("button").removeClass("layui-btn-warm").addClass("layui-btn-normal");
		objOkTab = null;
		okTab = layui.okTab({
			url: "/nav/list?mid=" + mid, modelId: mid, openTabNum: 30, parseData: function (data) {
				return data;
			}
		});
		objOkTab = okTab;
		okTab.render();
	});
	$(".sysModelLi:first").click();
	/**
	 * 系统设置
	 */
	$("body").on("click", "#okSetting", function () {
		layer.open({
			type: 2, title: "系统设置", shadeClose: true, closeBtn: 1, skin: "slideInRight ok-setting",
			area: ['340px', '100%'],
			offset: 'r',
			time: 200000,
			anim: -1,
			content: "/system/setting"
		});
	});
	/**
	 * 添加新窗口
	 */
	$("body").on("click", "#navBar .layui-nav-item a, #userInfo a", function () {
		if ($(this).siblings().length == 0) {
			okTab.tabAdd($(this));
		}
		$(this).parent("li").siblings().removeClass("layui-nav-itemed");
		if (!$(this).attr("lay-id")) {
			let topLevelEle = $(this).parents("li.layui-nav-item");
			let childs = $("#navBar > li > dl.layui-nav-child").not(topLevelEle.children("dl.layui-nav-child"));
			childs.removeAttr("style");
		}
	});
	/**
	 * 左侧菜单展开动画
	 */
	$("#navBar").on("click", ".layui-nav-item a", function () {
		if (!$(this).attr("lay-id")) {
			let superEle = $(this).parent();
			let ele = $(this).next('.layui-nav-child');
			let height = ele.height();
			ele.css({"display": "block"});
			// 是否是展开状态
			if (superEle.is(".layui-nav-itemed")) {
				ele.height(0);
				ele.animate({height: height + "px"}, function () {
					ele.css({height: "auto"});
				});
			} else {
				ele.animate({height: 0}, function () {
					ele.removeAttr("style");
				});
			}
		}
	});

	/**
	 * 左边菜单显隐功能
	 */
	$(".ok-menu").click(function () {
		$(".layui-layout-admin").toggleClass("ok-left-hide");
		$(this).find("i").toggleClass("ok-menu-hide");
		localStorage.setItem("isResize", false);
		setTimeout(function () {
			localStorage.setItem("isResize", true);
		}, 1200);
	});

	/**
	 * 移动端的处理事件
	 */
	$("body").on("click", ".layui-layout-admin .ok-left a[data-url], .ok-make", function () {
		if ($(".layui-layout-admin").hasClass("ok-left-hide")) {
			$(".layui-layout-admin").removeClass("ok-left-hide");
			$(".ok-menu").find('i').removeClass("ok-menu-hide");
		}
	});

	/**
	 * tab左右移动
	 */
	$("body").on("click", ".okNavMove", function () {
		let moveId = $(this).attr("data-id");
		let that = this;
		okTab.navMove(moveId, that);
	});

	/**
	 * 刷新当前tab页
	 */
	$("body").on("click", ".ok-refresh", function () {
		okTab.refresh(this, function (okTab) {
		});
	});

	/**
	 * 关闭tab页
	 */
	$("body").on("click", "#tabAction a", function () {
		let num = $(this).attr("data-num");
		okTab.tabClose(num);
	});

	/**
	 * 键盘的事件监听
	 */
	$("body").on("keydown", function (event) {
		event = event || window.event || arguments.callee.caller.arguments[0];
		// 按 Esc
		if (event && event.keyCode === 27) {
			console.log("Esc");
			$("#fullScreen").children("i").eq(0).removeClass("layui-icon-screen-restore");
		}
		// 按 F11
		if (event && event.keyCode == 122) {
			console.log("F11");
			$("#fullScreen").children("i").eq(0).addClass("layui-icon-screen-restore");
		}
	});
	/**
	 * 全屏/退出全屏
	 */
	$("body").on("click", "#fullScreen", function () {
		if ($(this).children("i").hasClass("layui-icon-screen-restore")) {
			screenFun(2).then(function () {
				$("#fullScreen").children("i").eq(0).removeClass("layui-icon-screen-restore");
			});
		} else {
			screenFun(1).then(function () {
				$("#fullScreen").children("i").eq(0).addClass("layui-icon-screen-restore");
			});
		}
	});

	/**
	 * 全屏和退出全屏的方法
	 * @param num 1代表全屏 2代表退出全屏
	 * @returns {Promise}
	 */
	function screenFun(num) {
		num = num || 1;
		num = num * 1;
		let docElm = document.documentElement;

		switch (num) {
			case 1:
				if (docElm.requestFullscreen) {
					docElm.requestFullscreen();
				} else if (docElm.mozRequestFullScreen) {
					docElm.mozRequestFullScreen();
				} else if (docElm.webkitRequestFullScreen) {
					docElm.webkitRequestFullScreen();
				} else if (docElm.msRequestFullscreen) {
					docElm.msRequestFullscreen();
				}
				break;
			case 2:
				if (document.exitFullscreen) {
					document.exitFullscreen();
				} else if (document.mozCancelFullScreen) {
					document.mozCancelFullScreen();
				} else if (document.webkitCancelFullScreen) {
					document.webkitCancelFullScreen();
				} else if (document.msExitFullscreen) {
					document.msExitFullscreen();
				}
				break;
		}

		return new Promise(function (res, rej) {
			res("返回值");
		});
	}

	/**
	 * 系统公告
	 */
	$(document).on("click", "#notice", noticeFun);
	function noticeFun() {
		layer.open({
			type: 0, title: "系统公告", btn: "我知道啦", btnAlign: 'c', content: okHoliday.getContent(), yes: function (index) {
				sessionStorage.setItem("notice", "true");
				layer.close(index);
			}, cancel: function (index) {
			}
		});
	}

	/**
	 * 退出操作
	 */
	$("#logout").click(function () {
		okLayer.sweetConfirm("确定要退出吗？", function (index) {
			okTab.removeTabStorage(function (res) {
				okTab.removeTabStorage();
				window.location = "/login/out";
			});
		});
	});

	/**
	 * 锁定账户
	 */
	let lock_inter = "";
	lockShowInit(okUtils);
	$("#lock").click(function () {
		okLayer.sweetConfirm("确定要锁定账户吗？", function (index) {
			layer.close(index);
			okUtils.local("isLock", '1');
			lockShowInit(okUtils);
		});
	});

	/**锁屏方法*/
	function lockShowInit(okUtils) {
		let localLock = okUtils.local("isLock");
		$("#lockPassword").val("");
		if (!localLock) {
			return;
		}
		$(".lock-screen").show();
		Snowflake("snowflake");
		let lock_bgs = $(".lock-screen .lock-bg img");
		$(".lock-content .time .hhmmss").html(okUtils.dateFormat("", "hh <p lock='lock'>:</p> mm"));
		$(".lock-content .time .yyyymmdd").html(okUtils.dateFormat("", "yyyy 年 M 月 dd 日"));
		let i = 0, k = 0;
		lock_inter = setInterval(function () {
			i++;
			if (i % 8 == 0) {
				k = k + 1 >= lock_bgs.length ? 0 : k + 1;
				i = 0;
				lock_bgs.removeClass("active");
				$(lock_bgs[k]).addClass("active");
			}
			$(".lock-content .time .hhmmss").html(okUtils.dateFormat("", "hh <p lock='lock'>:</p> mm"));
		}, 1000);
		//提交密码
		form.on('submit(lockSubmit)', function (data) {
			console.log(data);
			if (data.field.lock_password !== "123456") {
				layer.msg("密码不正确", {icon: 5, zIndex: 999999991});
			} else {
				layer.msg("密码输入正确", {
					icon: 6, zIndex: 999999992, end: function () {
						okUtils.local("isLock", null);
						$("#lockPassword").val("");   //清除输入框的密码
						$(".lock-screen").hide();
						clearInterval(lock_inter);
					}
				});
			}
			return false;
		});
		//退出登录
		$("#lockQuit").click(function () {
			window.location.href = "/login/out";
		});
	}
});
